//免登录api

const noLogin = [
	'api/common/init',
	'api/login/sendSmsCode',
	'api/login/mobileLogin',
	'api/login/wxMiniLogin',
	'api/index/photo',
	'api/index/index'
];

const install = (Vue, vm) => {

	vm.$cu.request.setConfig({});
	//请求拦截，配置Token等参数
	// type:request,upload
	vm.$cu.request.interceptor.request(function(config) {
		console.log(config)
		if (!noLogin.includes(config.data.action) && !vm.vuex_token) {
			vm.$vuex('vuex_user', {});
			uni.removeStorageSync('uni_id_token')
			uni.removeStorageSync('uni_id_token_expired')
			// #ifndef MP-WEIXIN
			vm.goPage('/pages/login/login');
			// #endif
			// #ifdef MP-WEIXIN
			vm.goPage('/pages/login/choose');
			// #endif
			return false;
		}
		return config;
	})
	//响应拦截，判断状态码是否通过
	vm.$cu.request.interceptor.response(function(res) {
		// console.log(res)
		if (['30204','30203'].includes(res.code)) {
			vm.$vuex('vuex_user', {});
			uni.removeStorageSync('uni_id_token')
			uni.removeStorageSync('uni_id_token_expired')
			vm.$cu.toast(res.message);
			setTimeout(() => {
				// #ifndef MP-WEIXIN
				vm.goPage('/pages/login/login');
				// #endif
				// #ifdef MP-WEIXIN
				vm.goPage('/pages/login/choose');
				// #endif
			}, 1000)
		}
	
		return res;
	})
	//上传文件
	vm.$api.upload = ({
		name,
		size,
		path,
		type
	} = param) => {
		return new Promise(async (resolve, reject) => {
			let y, m, d, day, r, cloudPath;
			d = new Date();
			y = d.getFullYear().toString();
			m = (d.getMonth() + 1).toString().padStart(2, '0');
			day = d.getDate().toString().padStart(2, '0');
			r = (vm.$cu.random(1, 9999)).toString().padStart(4, '0');
			cloudPath = y + '/' + m + '/' + day + '/' + Date.now() + r + '.' + name.split('.').pop();
			const result = await uniCloud.uploadFile({
				filePath: path,
				cloudPath,
				onUploadProgress(e) {
					console.log(e)
				}
			})			
			let url = '';
			if (/^cloud:\/\//.test(result.fileID)) {
				//腾讯云处理
				const {
					fileList
				} = await uniCloud.getTempFileURL({
					fileList: [result.fileID]
				})
				if (fileList.length) {
					url = fileList[0].tempFileURL || fileList[0].download_url;
				}
			} else {
				url = result.fileID;
			}
			//上传云储存成功处理【保存数据库】				
			if (url) {
				vm.$cu.request.send('api/index/add_img', 'sy-photo', {
					fileID: result.fileID,
					url,
					file_size: size,
					file_name: name,
					type: type || name.split('.').pop()
				}).then(res => {
					console.log(res)
					if (res.code == 0) {
						resolve(url)
					} else {
						reject(res.message);
					}
				});
			}else{
				reject();				
			}
		})
	}

	//api接口
	vm.$api.init 				= (params = {}) => vm.$cu.request.send('api/common/init', 'sy-photo', params);	
	vm.$api.sendSmsCode 		= (params = {}) => vm.$cu.request.send('api/login/sendSmsCode', 'sy-photo', params);
	vm.$api.mobileLogin 		= (params = {}) => vm.$cu.request.send('api/login/mobileLogin', 'sy-photo', params);
	vm.$api.wxMiniLogin 		= (params = {}) => vm.$cu.request.send('api/login/wxMiniLogin', 'sy-photo', params);
	vm.$api.getUserInfo 		= (params = {}) => vm.$cu.request.send('api/user/getUserInfo', 'sy-photo', params);
	vm.$api.getMyphoto 			= (params = {}) => vm.$cu.request.send('api/user/myphoto', 'sy-photo', params);
	vm.$api.delphoto 			= (params = {}) => vm.$cu.request.send('api/user/delphoto', 'sy-photo', params);
	vm.$api.getPhotosList	 	= (params = {}) => vm.$cu.request.send('api/index/photo', 'sy-photo', params);
	vm.$api.getPhotosIndex	 	= (params = {}) => vm.$cu.request.send('api/index/index', 'sy-photo', params);
	vm.$api.photosCreate	 	= (params = {}) => vm.$cu.request.send('api/index/create', 'sy-photo', params);
}

export default {
	install
}
